import WxValidate from '../../utils/WxValidate.js'

const app = getApp()

let Validate = ''

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    // canIUse: wx.canIUse('button.open-type.getUserInfo')
    user: wx.getStorageSync('current_user')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  handleaa(){
    wx.navigateTo({
      url: '../orders/orders',
    })
  },
  onLoad: function () {
    const rules = {
      name: {
        required: true
      },
      mobile: {
        required: true,
        tel: true
      }
    }

    const messages = {
      name: {
        required: '请输入姓名',
      },
      mobile: {
        required: '请输入手机号码',
        tel: '请输入正确的手机号码'
      }
    }
    Validate = new WxValidate(rules, messages)
    // app.showToast('l', false, 500, '../../assets/warning.png')
  },
  meWallet(e){
    app.query('/api/wallets/me' , {} , 'get').then( res => {
      if(res.status === 'success'){
        this.setData({
          me: res
        })
      }
    })
  },
  formSubmit: function (e) {
    const params = e.detail.value
    const { user } = this.data;
    
    let _remark = { "entityMap": {}, "blocks": [{ "key": "1k137", "text": "123", "type": "unstyled", "depth": 0, "inlineStyleRanges": [], "entityRanges": [], "data": {} }] };
    let _random;

    if (!Validate.checkForm(e)) {

      const error = Validate.errorList[0]
      app.showToast(error.msg, false, 500, '../../assets/warning.png')
      //提示信息  
      // console.log(error, e)
      return false
    }
    if(!!e.detail.value.remark){
      _remark.blocks[0].key = Math.floor( Math.random() * 1000 )
      _remark.blocks[0].text = e.detail.value.remark;
      _remark.date = new Date();
      _remark.name = wx.getStorageSync('current_user').name
      e.detail.value.remark = JSON.stringify(_remark);
      // { "entityMap":{ },"blocks": [{ "key": "1k137", "text": "123", "type": "unstyled", "depth": 0, "inlineStyleRanges": [], "entityRanges": [], "data": {} }]}
    }
    app.query('/api/clues', { clue: e.detail.value }, 'post').then(res => {
      // console.log(res)
      if (res.status == 'success') {
        // app.showToast('线索新建成功', 'success', 500)
        // setTimeout(() => {
        //   wx.navigateBack({
        //     delta: 1
        //   })
        // }, 600)
        wx.navigateTo({
          url: '../reaspon/reaspon?title=clues'
        })

      }else{
        let _val;
        _val = typeof res.msg == 'string' ? res.msg : res.msg[0]
        app.showToast(_val, false, 500, '../../assets/warning.png')
      }
    });
  },
  onShow(){
    this.meWallet();
    this.setData({
      userInfo: {}
    })
  }
})
